package view;

import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.event.MouseEvent;

import model.GameConfiguration;
import model.Slot;

public class PlayerTurnState extends BoardState {
	private int player;
	private Rectangle[] colsRect;

	public PlayerTurnState(GameConfiguration config, Board board, int player,
			Slot[][] slots, String p1, String p2, int lastPlayedRow, int lastPlayedCol) {
		super(config, board, slots, p1, p2, lastPlayedRow, lastPlayedCol);
		this.player = player;

		colsRect = new Rectangle[configuration.getCols()];
		for (int i = 0; i < configuration.getCols(); i++)
			colsRect[i] = new Rectangle(GraphicsUtils.gameBoardOffset + i
					* GraphicsUtils.cellSize, GraphicsUtils.gameBoardOffset
					+ GraphicsUtils.headerHeight, GraphicsUtils.cellSize,
					GraphicsUtils.cellSize * configuration.getRows());
	}

	@Override
	public void paintGame(Graphics2D g2d) {
		super.paintGame(g2d);

		// Draw Player Turn
		GraphicsUtils.paintPlayerTurn(configuration.getRows() * GraphicsUtils.cellSize + 2
				* GraphicsUtils.gameBoardOffset + GraphicsUtils.headerHeight,
				g2d, player);

		// Disegno indicazione inserimento
		if (board.getHoveredColumn() >= 0 && board.getHoveredColumn() < configuration.getCols())
			GraphicsUtils.paintHoveredColumn(g2d, player,
					board.getHoveredColumn());

	}

	@Override
	protected void mousePressed(MouseEvent e) {
		int col = 0;
		while (col < configuration.getCols() && !colsRect[col].contains(e.getPoint()))
			col++;
		if (col < configuration.getCols())
			board.getObservableBoard().columnSelected(col);

	}

	@Override
	protected void mouseMoved(MouseEvent e) {
		int col = 0;
		while (col < configuration.getCols() && !colsRect[col].contains(e.getPoint()))
			col++;
		if (col < configuration.getCols())
			board.columnHovered(col);
		else
			board.columnHovered(-1);
	}

}
